<?php
  $yhendus=new mysqli("localhost", "if13", "ifikad", "if13_alisiko1_k");
  if(isSet($_REQUEST["uusleht"])){
    $kask=$yhendus->prepare("INSERT INTO yritus (etteaste, aeg) VALUES (?, ?)");
    $kask->bind_param("ss", $_REQUEST["etteaste"], $_REQUEST["aeg"]);
    $kask->execute();
    header("Location: $_SERVER[PHP_SELF]");
    $yhendus->close();
    exit();
  }
  if(isSet($_REQUEST["kustutusid"])){
    $kask=$yhendus->prepare("DELETE FROM yritus WHERE id=?");
    $kask->bind_param("i", $_REQUEST["kustutusid"]);
    $kask->execute();    
  }
?>
<!doctype html>
<html>
  <head>
    <title>Teated lehel</title>
    <style type="text/css">
       #menyykiht{
         float: left;
         padding-right: 30px;
       }
       #sisukiht{
         float:left;
       }
       #jalusekiht{
         clear: left;
       }
    </style>
  </head>
  <body>
    <div id="menyykiht">
        <h2>Etteasted</h2>
        <ul>
          <?php
             $kask=$yhendus->prepare("SELECT id, etteaste FROM yritus");
             $kask->bind_result($id, $etteaste);
             $kask->execute();
             while($kask->fetch()){
               echo "<li><a href='?id=$id'>".
                          htmlspecialchars($etteaste)."</a></li>";
             }
          ?>
        </ul>
        <a href='?lisamine=jah'>Lisa etteaste..+</a>
    </div>
    <div id="sisukiht">
       <?php
         if(isSet($_REQUEST["id"])){
            $kask=$yhendus->prepare("SELECT id, etteaste, aeg FROM yritus
               WHERE id=? ORDER BY aeg");
            $kask->bind_param("i", $_REQUEST["id"]); 
            $kask->bind_result($id, $etteaste, $aeg);
            $kask->execute();
            if($kask->fetch()){
              echo htmlspecialchars($etteaste).' '.$aeg;
			  
			  echo "</br>";
              
              echo "<br /><a href='?kustutusid=$id'>kustuta</a>";
            } else {
              echo "Vigased andmed.";
            }
         } 
         if(isSet($_REQUEST["lisamine"])){
           ?>
             <form action='?'>
              <input type="hidden" name="uusleht" value="jah" />
              <h2>Uue teate lisamine</h2>
              <dl>
                <dt>Üritus:</dt>
                <dd>
                 <input type="text" name="etteaste" />
                </dd>
                <dt>Toimumise aeg:</dt>
                <dd>
                   <input type="time" name="aeg" />
                </dd>
				
               </dl>
               <input type="submit" value="sisesta">
             </form>
           <?php
         }
       ?>
    </div>
    <div id="jalusekiht">
       Lehe tegid Erik ja Ain
    </div>
  </body>
</html>
<?php
  $yhendus->close();
?>